// pages/home/home.js
import { getBannerList , getMenuList, getGoodsList} from '../../api/home'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bannerList: [], // 轮播图列表
    menuList: [], // 菜单列表
    goodsList: [],
    count: 1, // 当前页
    limitNum: 10, // 每页多少条
    hasMore: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getBannerListData()
    this.getMenuListData()
    this.getGoodsListData()
  },
  async getBannerListData() {
    const res = await getBannerList()

    this.setData({
      bannerList: res.data
    })
  },
  async getMenuListData() {
    const res = await getMenuList()

    this.setData({
      menuList: res.data
    })
  },
  // 获取商品列表
  async getGoodsListData() {
    const res = await getGoodsList({
      count: this.data.count,
      limitNum: this.data.limitNum
    })

    this.setData({
      hasMore: res.data.length > 0,
      goodsList: [...this.data.goodsList,...res.data]
    },() => {
      wx.stopPullDownRefresh()
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      goodsList: [],
      hasMore: true
    })
    this.data.count = 1
    this.getGoodsListData()
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (!this.data.hasMore) return

    this.data.count++
    this.getGoodsListData()
  },
  goToDetail(e){
    const {id} = e.currentTarget.dataset

    // 入栈的操作（同一个页面栈）
    wx.navigateTo({
      url: `/subpkg/goods-detail/goods-detail?proid=${id}`,
    })
  }
  /**
  getBannerListData2() {
    wx.request({
      url: 'https://huangjiangjun.top:3002/api/banner/list',
      success: res => {
        this.setData({
          bannerList: res.data.data
        })
      }
    })  
  },
  getMenuListData2() {
    wx.request({
      url: 'https://huangjiangjun.top:3002/api/menu/list',
      success: res => {
        this.setData({
          menuList: res.data.data
        })
      }
    })
  }
   */
})